package com.bw.user.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.user.config.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bw.user.entity.UserEntity;
import com.bw.user.service.UserService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;



/**
 * 
 *
 * @author jwcz
 * @email 2898807664@qq.com
 * @date 2022-03-24 15:52:51
 */
@RestController
@RequestMapping("user/user")
public class UserController {
    @Autowired
    private UserService userService;

    @SuppressWarnings("all")
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 登录
     */
    @RequestMapping("/login")
    public R login(@RequestParam("userTel") String userTel,
                   @RequestParam("pwd") String pwd){
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        if(userTel!=null) {
            wrapper.eq("user_tel",userTel);
            UserEntity entity = userService.getOne(wrapper);
            if(entity!=null) {
                if(!pwd.equals("") && pwd.equals(entity.getUserPwd())) {
                    String token = jwtUtil.createJWT(entity.getUserId() + "", "roles", "roles");
                    return R.ok().put("entity",entity).put("token",token);
                }else {
                    return R.error("密码错误");
                }
            }else {
                return R.error("该用户不存在");
            }
        }else {
            return R.error("该手机号未注册");
        }
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Integer userId){
		UserEntity user = userService.getById(userId);

        return R.ok().put("user", user);
    }
    /**
     * 信息
     */
    @RequestMapping("/getMine/{nikeName}")
    public R getMine(@PathVariable("nikeName") String nikeName){
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_nike_name",nikeName);
        UserEntity userEntity = userService.getOne(wrapper);
        return R.ok().put("user",userEntity);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] userIds){
		userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
